package com.example.hometown_walker.service.process;

/**
 * @description: 进度控制任务
 * @author: chenchaoran
 * @date: 2020-04-01
 */
public abstract class ProcessTask implements Runnable {

    private int process = 0;

    /**
     * 外部查询当前线程进度
     *
     * @return
     */
    public int getProcess() {
        return this.process;
    }

    /**
     * 用在子任务run方法中，可更细粒度地设置进度值
     *
     * @param process
     */
    protected void setProcess(int process) {
        if (process < 0) {
            return;
        }
        if (process > 100) {
            process = 100;
        }
        this.process = process;
    }

    /**
     * 线程用途
     *
     * @return
     */
    abstract String taskName();

    /**
     * 任务过程
     */
    abstract void doTask();

    @Override
    public final void run() {
        System.out.println(Thread.currentThread().getName() + "-开始.." + taskName());
        doTask();
        System.out.println(taskName() + "完成。");
    }
}
